
package com.sausageking.client.merchant.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sausageking.client.common.ClientUtil;
import com.sausageking.shared.Merchant;
import com.sausageking.shared.OfferUtil;

public class StickerViewImpl extends Composite implements StickerView {

  private static StickerViewImplUiBinder uiBinder = GWT
      .create(StickerViewImplUiBinder.class);

  interface StickerViewImplUiBinder extends UiBinder<Widget, StickerViewImpl> {
  }

  @SuppressWarnings("unused")
  private Presenter presenter;

  @UiField
  Image qr;

  @UiField
  Label businessName;

  @UiField
  Label rewardTrigger;

  @UiField
  Label shortReward;

  @UiField
  Label longReward;
  
  @UiField
  Label url;

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  @Inject
  OfferUtil offerUtil;

  public StickerViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @Override
  public void setMerchant(Merchant merchant) {
    ClientUtil util = new ClientUtil();
    String merchantUrl = util.getShortMerchantUrl(String.valueOf(merchant
        .getId()));

    String merchantUrlNoProtocol = util.getShortMerchantUrlNoProtocol(String
        .valueOf(merchant.getId()));

    qr.setUrl("https://chart.googleapis.com/chart?"
        + "chld=L|1&chs=400x400&cht=qr&chl=" + util.encode(merchantUrl));
    businessName.setText(merchant.getName());
    rewardTrigger.setText("buy " + merchant.getOffer().getCheckinCount()
        + " get 1");
    String reward = merchant.getOffer().getRewardDescription();
    if (reward.length() > 10 ) {
      shortReward.setVisible(false);
      longReward.setText(reward);
    } else {
      shortReward.setText(reward);
      longReward.setVisible(false);
    }
    url.setText(merchantUrlNoProtocol);
  }
}
